﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=1">
    <meta name="format-detection" content="telephone=no">
    <meta charset="UTF-8">
    <title>nextTick用法</title>
    <link rel="stylesheet" href="../css/bootstrap.css"/>
    <script src="../js/jquery-2.2.0.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
</head>
<body>
<h1>nextTick用法</h1><br/>

<div id="app">
    {{name}}<br/>
    <child ref="childInstance"></child>
</div>
<script src="../js/vue-2.5.16.js"></script>
<script src="../node_modules/axios/dist/axios.js"></script>
<script src="../node_modules/lodash/lodash.js"></script>

<script>
    var app = new Vue({
        el:'#app',
        data:{
            name:'模版数据'
        },
        mounted(){
            //看日志的时候发打出来的结果是   li里嵌套  1,2,3原因则是不管是父组件还是子组件的mounted()钩子都是异步的。
            // 要想等子组件先完成再执行父组件的mounted后续操作则需要使用nextTick
            //console.log(this.$refs.childInstance.$el.innerHTML);
            this.$nextTick(()=>{
                //这个时候打开浏览器发现打出来的日志终于是   li里嵌套  4,5,6啦
                console.log(this.$refs.childInstance.$el.innerHTML);
            })
        },
        components:{
            child:{
                template:'<div><ul><li v-for="v in arr">{{v}}</li></ul></div>',
                data(){
                    return {
                        arr:[1,2,3]
                    }
                },
                mounted(){
                    this.arr = [4,5,6]
                }
            }
        }
    });
</script>

</body>
</html>